package com.hmdp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Configuration
public class ThreadPoolExecutorConfiguration {
    @Bean
    public ThreadPoolExecutor getThreadPoolExecutor() {
//        线程工厂 用户制定为线程池创建新线程的方式
        ThreadFactory threadFactory = new ThreadFactory() {
            private int count = 1;

            @Override
            public Thread newThread(Runnable r) {
//                创建一个线程
                Thread thread = new Thread(r);
//                注意这个位置count自增1
                thread.setName("线程:" + count++);
                return thread;
            }
        };
//        阻塞队列的实现次数还是很多的。
//        keepAliveTime 意思就是空闲线程在无任务情况下的存活时间！
        ThreadPoolExecutor threadPoolExecutor
                = new ThreadPoolExecutor(
                3, 5, 100,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5), threadFactory);
        return threadPoolExecutor;
    }
}
